package com.tjakus.bean;

import java.util.Iterator;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.tjakus.hibernate.HibernateUtil;
import com.tjakus.hibernate.Settings;
import com.tjakus.logging.Logging;

public class SettingsBean {

	private List<Settings> settingsList = HibernateUtil.getAllSettings();;

	public List<Settings> getSettingsList() {
		Logging.getLogger().info("TOMISLAV");
		return settingsList;
	}

	public void setSettingsList(List<Settings> settingsList) {
		this.settingsList = settingsList;
	}

	public void submit(ActionEvent actionEvent) {
		Logging.getLogger().info("SUBMIT");
		FacesContext context = FacesContext.getCurrentInstance();
		for (Iterator<Settings> iter = settingsList.iterator(); iter.hasNext();) {
			Settings setting = iter.next();
			Logging.getLogger()
					.info(
							"SUBMIT - " + setting.getName() + " - [ " + setting.getValue()
									+ " ]");
		}
		boolean status = HibernateUtil.updateSettings(settingsList);
		if (status) {
			context.addMessage(null, new FacesMessage("Settings saved.")); 
		}
		else {
			context.addMessage(null, new FacesMessage("ERROR, settings NOT saved.")); 
		}
	}
}
